# 压缩zip函数
seq1 = ["foo","bar","baz"]
seq2 = ["one","two","three"]
zipper = zip(seq1,seq2)  # 保存在内存中
print(list(zipper))
zipper = zip(seq1,seq2)
for i,v in enumerate(zipper):
    print(i)
    print(v)
    print("------")
print(list(zipper))  # 空的
# 解压缩
pitchers = [("nolan","ryan"),("ylc","zqq")]
first,second = zip(*pitchers)
print(first)
print(second)

[('foo', 'one'), ('bar', 'two'), ('baz', 'three')]
0
('foo', 'one')
------
1
('bar', 'two')
------
2
('baz', 'three')
------
[]
('nolan', 'ylc')
('ryan', 'zqq')


def demo(*args,**kwargs):
    print(args)
    print(kwargs)
gl_list = [1,2,3,4]
gl_dict = {"name":"ylc","world":99}
demo(1,2,3,4,*gl_list,**gl_dict)

(1, 2, 3, 4, 1, 2, 3, 4)
{'name': 'ylc', 'world': 99}


# 列表、集合、字典推导式
num = [x+1 for x in range(21) if x%2==0]
print(f"num变量是:{num}")

strings = ["yysfdfdfyhnldfohq1083","23","34","2323","4546"]
unique_string = {len(x) for x in strings}
print(f"唯一变量数量是：{unique_string}")
# 等价与
set(map(len,strings))

loc_mapping = {values:i for i ,values in enumerate(strings)}
print(loc_mapping)

some_tuples = [(1,2,4),(4,5,6),(7,88,9)]
num_list = [[x for x in tup] for tup in some_tuples]
print(f"num_list变量：{num_list}")

num变量是:[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
唯一变量数量是：{2, 4, 21}
{'yysfdfdfyhnldfohq1083': 0, '23': 1, '34': 2, '2323': 3, '4546': 4}
num_list变量：[[1, 2, 4], [4, 5, 6], [7, 88, 9]]


# 嵌套列表推导式与上面的要区分
all_data =[["jhon","Emily","Michael","Mary","Steveen"],["Maria","Juan","Javier","eefdg","pileve","ylc"]]
# 方法一：
names_of_interest = []
for names in all_data:
    enough_es = [name for name in names if name.count("e") >=2]
    names_of_interest.extend(enough_es)
else:
    print(names_of_interest)
# 方法二：
result = [name for names in all_data for name in names if name.count("e")>=2]
print("result:{}".format(result))

['Steveen', 'eefdg', 'pileve']
result:['Steveen', 'eefdg', 'pileve']


# 函数迭代function操作
import re
states = [" alab","yljf~!","##?44gg","?ioyhohsf"]
# 需要去除：空格、#、？
def remove_punctuation(value):
    return re.sub("[!#?]","",value)
clean_ops = [str.strip,remove_punctuation,str.title]  # 迭代函数名称 不带()

def clean_strings(strings,ops):  # strings形参列表、ops准备要迭代 函数
    result = []
    for value in strings:
        for function in ops:
            value = function(value)
        result.append(value)
    return result
clean_strings(states,clean_ops)    

['Alab', 'Yljf~', '44Gg', 'Ioyhohsf']


strings = ["foo","card","bar","aaaa","abab"]
strings.sort(key=lambda x : len(set(list(x))),reverse = True) # 按照每个元素从多到底排序
print(strings)
# 柯里化
def add_numbers(x,y):
    return x+y
y = 1.6
add_five = lambda y : add_numbers(5,y)  # 在已有add_numbers建立新函数，add_five是函数
add_five(1)

['card', 'bar', 'foo', 'abab', 'aaaa']

6